﻿using HidLibrary;
using System.Diagnostics;
using System.Management;

namespace TroubleshootingofBeanBagAssistant
{
	/// <summary>
	/// 获取Usb序列号
	/// </summary>
	public class USBHelper
	{

		/// <summary>
		/// 获取网卡的序列号
		/// </summary>
		/// <returns></returns>
		public static string GetMac()
		{
			try
			{
				//创建ManagementClass对象
				ManagementClass mc = new("Win32_NetworkAdapterConfiguration");
				ManagementObjectCollection moc = mc.GetInstances();
				string macAddress = string.Empty;
				foreach (ManagementObject mo in moc)//遍历获取的集合
				{
					if ((bool)mo["IPEnabled"])//判断IPEnabled的属性是否为true
					{
						macAddress = mo["MacAddress"].ToString();//获取网卡的序列号

					}
				}
				return macAddress;


				//-----------------------------

				return "";


			}
			catch (Exception)
			{
				//这里写异常的处理（最好写入日志文件）
				return "";
			}
		}

		public static List<string> GetHidMouserIDList()
		{
			List<string> list = new();
			List<ManufacturerModel> listdata = new();
			try
			{
				string HidMouserID = "";
				//  

				if (false)
				{

				}
				else
				{

					#region 本地

					// 供应商ID和产品ID 1E9D   0867
					int vendorId = 0x1E9D;
					int productId = 0x0867;
					int[,] datapidvid = { { 0x1E9D, 0x0867, } };



					int Length = datapidvid.Length / 2;

					for (int i = 0; i < Length; i++)
					{
						vendorId = datapidvid[i, 0];
						productId = datapidvid[i, 1];
						HidDevice? device = HidDevices.Enumerate(vendorId, productId, vendorId, productId).FirstOrDefault();
						if (device != null)
						{
							HidMouserID = device.DevicePath;
							list.Add(HidMouserID);
						}
					}
					#endregion

				}
				if (HidMouserID != "")
				{

				}
				return list;
			}
			catch (Exception ex)
			{
				//LogWriter.Log(ex.Message, "GetHidMouserIDList");
				return null;
			}
		}

		/// <summary>
		/// 获取设备的序列号
		///  读取规则  取设备实例路径例如 
		///        
		/// </summary>
		/// <returns></returns>
		public static async Task<List<string>> GetHidMouserID()
		{
			List<string> list = new();
			List<ManufacturerModel> listdata = new();
			try
			{
				string HidMouserID = "";
				//  

				if (false)
				{


				}
				else
				{
					#region 本地


					// 供应商ID和产品ID
					int vendorId = 0x248A;
					int productId = 0x6092;

					int[,] datapidvid = { { 0x046D, 0xC09D, }, { 0x369b, 0x0064, }, { 0xABC8, 0xCA88, }, { 0x05AC, 0x024F }, { 0x24AE, 0x1802 }, { 0xABC8, 0xCA08 }
						, { 0x03F6, 0x0092 } , { 0x0A12, 0x0001 }, { 0x0103f6, 0x0033 }, { 0x248A, 0xCA08 },{ 0x248A,0x8514},{ 0x10D6,0xB008},{ 0x0D8C,0x0312},{ 0x248A,0x6092}
					};



					int Length = datapidvid.Length / 2;



					for (int i = 0; i < Length; i++)
					{

						vendorId = datapidvid[i, 0];
						productId = datapidvid[i, 1];

						HidDevice? device = HidDevices.Enumerate(vendorId, productId, vendorId, productId).FirstOrDefault();
						if (device != null)
						{
							HidMouserID = device.DevicePath;
							list.Add(vendorId.ToString());
							list.Add(productId.ToString());

							break;
						}
					}

					#endregion
				}
			}
			catch (Exception ex)
			{
				//LogWriter.Log(ex.Message, "GetHidMouserID");
				return null;
			}
			return list;
		}



		/// <summary>
		/// 从DeviceID设备解析串行
		/// </summary>
		/// <param name="deviceId"></param>
		/// <returns></returns>

		public static string ParseSerialFromDeviceIDmouse(string deviceId)
		{
			try
			{
				string[] splitDeviceId = deviceId.Split('\\');

				int arrayLen = splitDeviceId.Length - 1;
				string[] serialArray = null;

				if (splitDeviceId[arrayLen].Length > 8)
				{
					serialArray = splitDeviceId[arrayLen].Split('&');

				}
				else
				{
					serialArray = splitDeviceId[3].Split('&');
					arrayLen = 3;
				}


				string[] serialArray1 = splitDeviceId[arrayLen].Split('#');
				string[] serialArray2 = serialArray1[1].Split('_');

				string pid = serialArray2[1].Split('&')[0];
				string vid = serialArray2[2].Split('&')[0];

				if (serialArray.Length >= 5)
				{
					if (serialArray.Length == 6)
					{
						string serial = serialArray[3];
						serial = (pid + vid + serial).ToUpper();
						return serial;
					}
					else
					{
						string serial = serialArray[4];
						serial = (pid + vid + serial).ToUpper();
						return serial;
					}
				}
				else
				{
					return "";
				}
			}
			catch (Exception ex)
			{
				//LogWriter.Log(ex.Message, "ParseSerialFromDeviceIDmouse");
				return "";
			}

		}
		/// <summary>
		/// 从DeviceID设备解析串行
		/// </summary>
		/// <param name="deviceId"></param>
		/// <returns></returns>

		public static string ParseSerialFromDeviceIDmouseBluetooth(string deviceId)
		{
			try
			{
				string[] splitDeviceId = deviceId.Split('\\');

				int arrayLen = splitDeviceId.Length - 1;
				string[] serialArray = null;

				if (splitDeviceId[arrayLen].Length > 8)
				{
					serialArray = splitDeviceId[arrayLen].Split('&');
					return serialArray[3];
				}
				else
				{
					return "";
				}
			}
			catch (Exception ex)
			{
				//LogWriter.Log(ex.Message, "ParseSerialFromDeviceIDmouseBluetooth");
				return "";
			}
		}




		/// <summary>
		/// //整机序列号
		/// </summary>
		/// <returns></returns>
		public static string GetCpuID()
		{
			//获取整机列号
			string sn = "";
			try
			{
				// 定义WMI查询，用于获取CPU序列号
				string query = "SELECT * FROM Win32_Processor";
				ManagementObjectSearcher searcher = new(query);
				ManagementObjectCollection processors = searcher.Get();
				foreach (ManagementBaseObject? item in processors)
				{
					string serialNumber = item["ProcessorId"].ToString();
					Debug.WriteLine("CPU序列号: " + serialNumber);
					sn = serialNumber;
					break;
				}


			}
			catch (Exception ex)
			{
				Debug.WriteLine("发生错误: " + ex.Message);
			}
			return sn;
		}



		/// <summary>
		/// 机械码
		/// </summary>
		/// <returns></returns>
		public static string GetMachineCodeString()
		{
			string machineCodeString = "PC|" + GetCpuInfo() + "|" + GetMoAddress();
			return machineCodeString;
		}

		///   <summary> 
		///   获取cpu序列号     
		///   </summary> 
		///   <returns> string </returns> 
		public static string GetCpuInfo()
		{
			string cpuInfo = "";
			try
			{
				using (ManagementClass cimobject = new("Win32_Processor"))
				{
					ManagementObjectCollection moc = cimobject.GetInstances();

					foreach (ManagementObject mo in moc)
					{
						cpuInfo = mo.Properties["ProcessorId"].Value.ToString();
						mo.Dispose();
					}
				}
			}
			catch (Exception ex)
			{
				//LogWriter.Log(ex.Message, "GetCpuInfo");
			}
			return cpuInfo.ToString();
		}

		///   <summary> 
		///   获取硬盘ID     
		///   </summary> 
		///   <returns> string </returns> 
		public static string GetHDid()
		{
			string HDid = "";
			try
			{
				using (ManagementClass cimobject1 = new("Win32_DiskDrive"))
				{
					ManagementObjectCollection moc1 = cimobject1.GetInstances();
					foreach (ManagementObject mo in moc1)
					{
						HDid = (string)mo.Properties["Model"].Value;
						mo.Dispose();
					}
				}
			}
			catch (Exception ex)
			{
				//LogWriter.Log(ex.Message, "GetHDid");
			}
			return HDid.ToString();
		}

		///   <summary> 
		///   获取网卡硬件地址 
		///   </summary> 
		///   <returns> string </returns> 
		public static string GetMoAddress()
		{
			string MoAddress = "";
			try
			{
				using (ManagementClass mc = new("Win32_NetworkAdapterConfiguration"))
				{
					ManagementObjectCollection moc2 = mc.GetInstances();
					foreach (ManagementObject mo in moc2)
					{
						if ((bool)mo["IPEnabled"] == true)
						{
							MoAddress = mo["MacAddress"].ToString();
						}

						mo.Dispose();
					}
				}
			}
			catch (Exception ex)
			{
				//LogWriter.Log(ex.Message, "GetMoAddress");
			}
			return MoAddress.ToString();
		}




	}

	/// <summary>
	/// 制造商
	/// </summary>
	public class ManufacturerModel
	{
		public string vendorId { get; set; }
		public string productId { get; set; }

	}

}
